import React from 'react'
import BaseComponent from 'basecomponent'
import LinkNavbar from 'link-navbar'
import apis from 'apis'
import {withRouter} from 'react-router'
import './style.scss'
import ImageList from 'image-list'
import {bannerType} from 'utils/staticType'
class Banner extends BaseComponent {
  constructor() {
    super(...arguments);
    this.state= {
      recommendData: [],
      categoryData: [],
      qualityData: [],
      jobData: [],
      advData: []
    }
  }
  componentDidMount() {
    this.getAdvList(bannerType.recommend)
    this.getAdvList(bannerType.category)
    this.getAdvList(bannerType.quality)
    this.getAdvList(bannerType.job)
    this.getAdvList(bannerType.adv)
  }
  getAdvList(type){
    this.bannerRP = apis.banner.getBannerList(type);
    this.registerRequest(this.bannerRP.request);
    this.bannerRP.promise
      .then((res)=>{
        switch (type) {
          case bannerType.recommend:
            this.setState({recommendData: res.Data})
            break
          case bannerType.category:
            this.setState({categoryData: res.Data})
            break
          case bannerType.quality:
            this.setState({qualityData: res.Data})
            break
          case bannerType.job:
            this.setState({jobData: res.Data})
            break
          case bannerType.adv:
            this.setState({advData: res.Data})
            break
        }
      })
      .catch((err)=>{
        alert(err.msg)
      })
  }
  handleDelete(type, deleteData) {
    if(deleteData && deleteData.length > 0){
      deleteData.forEach((item) => {
        this.deleteBannerRP = apis.banner.deleteBanner(item.Id);
        this.registerRequest(this.deleteBannerRP.request);
        this.deleteBannerRP.promise
          .then((res)=>{
            this.getAdvList(type)
          })
          .catch((err)=>{
            alert(err.msg)
          })
      })
    }
  }
  render() {
    const {children} = this.props;
    const {recommendData, categoryData, qualityData, jobData, advData} = this.state;

    return (
      <div className={'sem-main-content'}>
        <div className={'class-banner-page'}>
          <LinkNavbar props={this.props}/>
          {children ? children :
            <div className={'class-banner-content'}>
              <ImageList
                class={'recommend-list'}
                title={'推荐'}
                type={bannerType.recommend}
                sightsData={recommendData}
                onDeleteClick={(type, deleteData) => {
                  this.handleDelete(type, deleteData)
                }}
                onSightClick={(item) => {
                  let path={
                    pathname:'/home/revise/banner',
                    query:{
                      type: item.Type,
                      Id: item.Id,
                    },
                    state:{
                      revise: false,
                      bannerData: item
                    }
                  };
                  this.props.router.push(path);
                }}
                onAddClick={(type) => {
                  let path={
                    pathname:'/home/add/banner',
                    query:{
                      type: type
                    },
                    state:{
                      revise: false
                    }
                  };
                  this.props.router.push(path);
                }}
              />
              <ImageList
                class={'category-list'}
                title={'分类'}
                type={bannerType.category}
                sightsData={categoryData}
                onDeleteClick={(type, deleteData) => {
                  this.handleDelete(type, deleteData)
                }}
                onSightClick={(item) => {
                  let path={
                    pathname:'/home/revise/banner',
                    query:{
                      type: item.Type,
                      Id: item.Id,
                    },
                    state:{
                      revise: false,
                      bannerData: item
                    }
                  };
                  this.props.router.push(path);
                }}
                onAddClick={(type) => {
                  let path={
                    pathname:'/home/add/banner',
                    query:{
                      type: type
                    },
                    state:{
                      revise: false
                    }
                  };
                  this.props.router.push(path);
                }}
              />
              <ImageList
                class={'quality-list'}
                title={'精品'}
                type={bannerType.quality}
                sightsData={qualityData}
                onDeleteClick={(type, deleteData) => {
                  this.handleDelete(type, deleteData)
                }}
                onSightClick={(item) => {
                  let path={
                    pathname:'/home/revise/banner',
                    query:{
                      type: item.Type,
                      Id: item.Id,
                    },
                    state:{
                      revise: false,
                      bannerData: item
                    }
                  };
                  this.props.router.push(path);
                }}
                onAddClick={(type) => {
                  let path={
                    pathname:'/home/add/banner',
                    query:{
                      type: type
                    },
                    state:{
                      revise: false
                    }
                  };
                  this.props.router.push(path);
                }}
              />
              <ImageList
                class={'job-list'}
                title={'就业'}
                type={bannerType.job}
                sightsData={jobData}
                onDeleteClick={(type, deleteData) => {
                  this.handleDelete(type, deleteData)
                }}
                onSightClick={(item) => {
                  let path={
                    pathname:'/home/revise/banner',
                    query:{
                      type: item.Type,
                      Id: item.Id,
                    },
                    state:{
                      revise: false,
                      bannerData: item
                    }
                  };
                  this.props.router.push(path);
                }}
                onAddClick={(type) => {
                  let path={
                    pathname:'/home/add/banner',
                    query:{
                      type: type
                    },
                    state:{
                      revise: false
                    }
                  };
                  this.props.router.push(path);
                }}
              />
              <ImageList
                class={'adv-list'}
                title={'校&企'}
                type={bannerType.adv}
                sightsData={advData}
                onDeleteClick={(type, deleteData) => {
                  this.handleDelete(type, deleteData)
                }}
                onSightClick={(item) => {
                  let path={
                    pathname:'/home/revise/banner',
                    query:{
                      type: item.Type,
                      Id: item.Id,
                    },
                    state:{
                      revise: false,
                      bannerData: item
                    }
                  };
                  this.props.router.push(path);
                }}
                onAddClick={(type) => {
                  let path={
                    pathname:'/home/add/banner',
                    query:{
                      type: type
                    },
                    state:{
                      revise: false
                    }
                  };
                  this.props.router.push(path);
                }}
              />
            </div>}
        </div>
      </div>
    );
  }
}

module.exports = withRouter(Banner);
